<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="/WEB-INF/c.tld" %>
<%@ taglib prefix="fmt" uri="/WEB-INF/fmt.tld" %>
<table width="100%" >
	<thead>
		<tr>
			<th>分类名称</th><th>添加子分类</th><th>操作</th>
		</tr>
	</thead>
		<c:if test="${obj !=null}">
			<c:forEach items="${obj}" var="o1" varStatus="st">
					<c:if test="${o1.parentid==0}">
						<tbody align="center">
					<tr>
						<td><input name="classifyName" value="${o1.classifyName }"/>
							<input name="parentid" value="${o1.parentid }" type="hidden"/>
							<input name="id" type="hidden" value="${o1.id }"/>
						</td>
						<th><a href="javascript:;" onclick="addsubitem(this);">添加子分类</a></th>
						<td><a href="javascript:;" onclick="upparentitem(this);">上移</a>
							<a href="javascript:;" onclick="downparentitem(this);">下移</a>
							<a href="javascript:;" onclick="deleteparentitem(this);">删除</a>
						</td>
					</tr>					
					<c:forEach items="${obj}" var="o2">
						<c:if test="${o1.id==o2.parentid}">
						<tr>
							<td>|_<input name="classifyName" value="${o2.classifyName }"/>
							<input name="parentid" value="${o2.parentid }" type="hidden"/>
							<input value="${o2.id }" name="id" type="hidden"/>
							</td>
							<td></td>
							<td>
							<a href="javascript:;" onclick="upsubitem(this);">上移</a>
							<a href="javascript:;" onclick="downsubitem(this);">下移</a>
							<a href="javascript:;" onclick="deletesubitem(this);">删除</a>
							</td>
						</tr>				
						</c:if>
					</c:forEach>				
					</tbody>
					</c:if>
			</c:forEach>
		</c:if>
			<tbody align="center">
				<tr>
					<td><a href="javascript:;" onclick="addparentitem(this);">添加分类</a></td><td><button type="button" onclick="saveallitem(this)">保存</button></td><td></td>
				</tr>
			</tbody>
</table>
<form id="saveform" action="${contextPath }/admin/classify/update.do" method="post">
	<input name="classifys" value="" type="hidden"/>
</form>
<script type="text/javascript">
<!--
	function deleteparentitem(obj){
		$(obj).parents("tbody").remove();
	}
	function deletesubitem(obj){
		$(obj).parents("tr").remove();
	} 

	function upparentitem(obj){
		var $currentitem = $(obj).parents("tbody");
		if($currentitem.prev("tbody").length==1){
			$currentitem.prev("tbody").before($currentitem);
		}
	}
	function upsubitem(obj){
		var $currentitem = $(obj).parents("tr");
		if($currentitem.prev("tr").length==1&&$currentitem.prev("tr").prev("tr").length==1){
			$currentitem.prev("tr").before($currentitem);
		}
	}
	function downparentitem(obj){
		var $currentitem = $(obj).parents("tbody");
		if($currentitem.next("tbody").length==1&&$currentitem.next("tbody").next("tbody").length==1){
			$currentitem.next("tbody").after($currentitem);
		}
	}
	function downsubitem(obj){
		var $currentitem = $(obj).parents("tr");
		if($currentitem.next("tr").length==1){
			$currentitem.next("tr").after($currentitem);
		}
	}
	function addparentitem(obj){
		var $additem = $("<tbody align='center'><tr><td><input name='classifyName' value=''/><input name='parentid' value='0' type='hidden'/><input name='id' value='0' type='hidden'/></td><td></td><td><a href='javascript:;' onclick='upparentitem(this);'>上移</a> <a href='javascript:;' onclick='downparentitem(this);'>下移</a> <a href='javascript:;' onclick='deleteparentitem(this);'>删除</a></td></tr></tbody>");
		$(obj).parents("table").find("tbody:last").before($additem);
	}
	function addsubitem(obj){
		var parentid = $(obj).parents("tbody").find("tr:first").find("td:eq(0)").find("input:eq(2)").val();
		var $additem = 	$("<tr><td>|_<input name='classifyName' value=''/><input name='parentid' value='"+parentid+"' type='hidden'/><input name='id' value='0' type='hidden'/></td><td></td><td><a href='javascript:;' onclick='upsubitem(this);'>上移</a> <a href='javascript:;' onclick='downsubitem(this);'>下移</a> <a href='javascript:;' onclick='deletesubitem(this);'>删除</a></td></tr>");		
		var $currentitem = $(obj).parents("tbody").append($additem);
	}

	function saveallitem(obj){
		var $table = $(obj).parents("table");
		var $trs = $table.find("tr:not(last)");
		var result = '[';
		var index = 0;
		var first = true;
		for(var i=0 ;i<$trs.length;i++){
			var tr = $trs[i];
			var $inputs = $(tr).find("input");
			if($inputs.length==0) continue;
			if(first){ 
				first = false;
			}else
				result += ",";
			result +="{";
			var first2 = true;
			for(var j=0;j<$inputs.length;j++){
				var input = $inputs[j];
				if(first2){
					first2=false;
			    }else{
					result += ",";
				}
			    result += $(input).attr("name");
			    result += ":";
			    result += $(input).val();
			}			
			if(first2){
				first2= false;
			}else{
				result += ",";
			}
			result +="classifyOrder:"+(index++)+"}";
		}
		result+="]";
		$("#saveform").find("input").val(result);
		$("#saveform").submit();
		return true;
	}
//-->
</script>

